/**
 * @author zhaozhenjiang
 * 创建数据库软件安装环境
 * */

package com.auto.oracle;

final class installShell {
	private createFile shell = null;
	private String BASE_PATH = null;
	private createParams para = null;
	private String[] oraclezip = null;
	public installShell(String base_path,String shell_name,createParams para){
		shell = new createFile(base_path, shell_name);
		BASE_PATH = base_path;
		this.para = para;
		if(para.getDbtype().equals("11.2.0.1")){
			oraclezip = new String[]{"linux.x64_11gR2_database_1of2.zip",
									 "linux.x64_11gR2_database_2of2.zip"};
		}
		else if(para.getDbtype().equals("11.2.0.4")){
			oraclezip = new String[]{"linux.x64_112040_database.zip"};
		}
		else if(para.getDbtype().equals("12.1.0.2")){
			oraclezip = new String[]{"linuxamd64_12102_database.zip"};
		}
		else if(para.getDbtype().equals("12.2.0.1")){
			oraclezip = new String[]{"linuxx64_12201_database.zip"};
		}
		else if(para.getDbtype().equals("12.2.0.2")){
			oraclezip = new String[]{"linux.x64_180000_db_home.zip"};
		}
		else if(para.getDbtype().equals("12.2.0.3")){
			oraclezip = new String[]{"linux.x64_193000_db_home.zip"};
		}
	}
	
	public void createFile(){
		shell.init();
		shell.append("#/bin/sh!");
		shell.append("rpm -Uvh "+ BASE_PATH + "/ct" + para.getOsCode() + "_rpm/*.rpm --nodeps --force");
		shell.append("mkdir -p /tmp/.oracle");
		shell.append("mkdir -p /var/tmp/.oracle");
		shell.append("chmod 1777 /var/tmp/.oracle");
		shell.append("chmod 1777 /tmp/.oracle");
		shell.append("groupadd oinstall");
		shell.append("groupadd dba");
		shell.append("groupadd oper");
		shell.append("groupadd asmdba");
		shell.append("useradd -g oinstall -G dba,asmdba,oper oracle");
		shell.append("mount -o remount,size=" + para.getShm() + "G /dev/shm");
		shell.append("echo 'mount -o remount,size=" + para.getShm() + "G /dev/shm' >> /etc/rc.d/rc.local");
		shell.append("echo 'su oracle -lc \"" + para.getOraclePath() + "/app/oracle/product/" + para.getVersion() + "/db_1/bin/lsnrctl start\"' >> /etc/rc.d/rc.local");
		shell.append("echo 'su oracle -lc " + para.getOraclePath() + "/app/oracle/product/" + para.getVersion() + "/db_1/bin/dbstart' >> /etc/rc.d/rc.local");
		shell.append("chmod +x /etc/rc.d/rc.local");
		if(para.getHasDev()){
			shell.append("mkdir " + para.getOraclePath());
			shell.append("mkfs.ext4 " + para.getInstallDevPath() + " -F");
			shell.append("echo \"" + para.getInstallDevPath() + "  " + para.getOraclePath() + "  ext4  defaults 0 0\" >> /etc/fstab");
			shell.append("mount -a");
			shell.append("rm -rf " + para.getOraclePath() + "/*");
		}
		shell.append("mkdir -p " + para.getOraclePath() + "/app/oracle");
		shell.append("chown -R oracle:oinstall " + para.getOraclePath());
		shell.append("chown -R oracle:oinstall " + para.getOraclePath() + "/app");
		shell.append("chmod -R 755 " + para.getOraclePath());
		shell.append("su - oracle << EOF");
		shell.append("echo \"export ORACLE_UNQNAME=" + para.getInstanceName() + "\" >> ~/.bash_profile;");
		shell.append("echo \"export ORACLE_TERM=xterm\" >> /home/oracle/.bash_profile;");
		shell.append("echo \"export ORACLE_BASE=" + para.getOraclePath() + "/app/oracle\" >> /home/oracle/.bash_profile;");
		shell.append("echo 'export ORACLE_HOME=\\$ORACLE_BASE/product/" + para.getVersion() + "/db_1' >> /home/oracle/.bash_profile;");
		shell.append("echo \"export ORACLE_SID=" + para.getInstanceName() + "\" >> /home/oracle/.bash_profile;");
		shell.append("echo 'export PATH=\\$ORACLE_HOME/bin:/usr/sbin:\\$PATH' >> /home/oracle/.bash_profile;");
		if(para.getOsCode() == 8)
			shell.append("echo 'export CV_ASSUME_DISTID=RHEL7.6' >> /home/oracle/.bash_profile;");
		shell.append("echo 'export LD_LIBRARY_PATH=\\$ORACLE_HOME/lib:/lib:/usr/lib' >> /home/oracle/.bash_profile;");
		shell.append("echo 'export CLASSPATH=\\$ORACLE_HOME/JRE:\\$ORACLE_HOME/jlib:\\$ORACLE_HOME/rdbms/jlib' >> /home/oracle/.bash_profile;");
		shell.append("mkdir -p " + para.getOraclePath() + "/app/oracle/product/" + para.getVersion() + "/db_1");
		shell.append("exit;");
		shell.append("EOF");
		shell.append("echo \"#limit.conf\" > /etc/security/limits.conf");
		shell.append("echo \"oracle           soft    nproc   2047\" >> /etc/security/limits.conf");
		shell.append("echo \"oracle           hard    nproc   16384\" >> /etc/security/limits.conf");
		shell.append("echo \"oracle           soft    nofile  1024\" >> /etc/security/limits.conf");
		shell.append("echo \"oracle           hard    nofile  65536\" >> /etc/security/limits.conf");
		if(para.getOsCode() == 8){
			shell.append("echo \"oracle   hard   memlock    134217728\" >> /etc/security/limits.conf");
			shell.append("echo \"oracle   soft   memlock    134217728\" >> /etc/security/limits.conf");
		}
		shell.append("echo \"#sysctl.conf\" > /etc/sysctl.conf");
		shell.append("echo \"fs.aio-max-nr = 1048576\" >> /etc/sysctl.conf");
		shell.append("echo \"fs.file-max = " + para.getFilemax() + "\" >> /etc/sysctl.conf");
		shell.append("echo \"kernel.shmall = " + para.getShmall() + "\" >> /etc/sysctl.conf");
		shell.append("echo \"kernel.shmmax = " + para.getShmax() + "\" >> /etc/sysctl.conf");
		shell.append("echo \"kernel.shmmni = 4096\" >> /etc/sysctl.conf");
		shell.append("echo \"kernel.sem = " + para.getSem1() + " " + para.getSem2() + " " + para.getSem3() + " " + para.getSem4() + "\" >> /etc/sysctl.conf");
		shell.append("echo \"net.ipv4.ip_local_port_range = 9000 65500\" >> /etc/sysctl.conf");
		shell.append("echo \"net.core.rmem_default = 262144\" >> /etc/sysctl.conf");
		shell.append("echo \"net.core.rmem_max = 4194304\" >> /etc/sysctl.conf");
		shell.append("echo \"net.core.wmem_default = 262144\" >> /etc/sysctl.conf");
		shell.append("echo \"net.core.wmem_max = 1048576\" >> /etc/sysctl.conf");
		if(para.getOsCode() == 8){
			shell.append("echo \"kernel.panic_on_oops = 1\" >> /etc/sysctl.conf");
			shell.append("echo \"net.ipv4.conf.all.rp_filter = 2\" >> /etc/sysctl.conf");
			shell.append("echo \"net.ipv4.conf.default.rp_filter = 2\" >> /etc/sysctl.conf");
		}
		shell.append("echo \"#selinux\" > /etc/selinux/config");
		shell.append("echo \"SELINUX=disabled\" >> /etc/selinux/config");
		shell.append("echo \"SELINUXTYPE=targeted\" >> /etc/selinux/config");
		shell.append("echo \"" + para.getOraclePwd() + "\" | passwd --stdin \"oracle\"");
		if(para.getOsCode() == 6){
			shell.append("/etc/init.d/iptables stop");
			shell.append("chkconfig iptables off");
		}
		else{
			shell.append("systemctl stop firewalld.service");
			shell.append("systemctl disable firewalld.service");
			shell.append("echo \"session required /lib64/security/pam_limits.so\" >> /etc/pam.d/login");
			shell.append("echo \"session required pam_limits.so\" >> /etc/pam.d/login");
			shell.append("echo \"if [ $USER = \"oracle\" ]; then\" >> /etc/profile");
			shell.append("echo \"	if [ $SHELL = \"/bin/ksh\" ]; then\" >> /etc/profile");
			shell.append("echo \"		ulimit -p 16384\" >> /etc/profile");
			shell.append("echo \"		ulimit -n 65536\" >> /etc/profile");
			shell.append("echo \"	else\" >> /etc/profile");
			shell.append("echo \"		ulimit -u 16384 -n 65536\" >> /etc/profile");
			shell.append("echo \"	fi\" >> /etc/profile");
			shell.append("echo \"fi\" >> /etc/profile");
			shell.append("source /etc/profile");
			shell.append("echo never > /sys/kernel/mm/transparent_hugepage/enabled");
		}
		shell.append("echo \"*******************************************************\"");
		shell.append("id oracle");
		shell.append("echo \"*******************************************************\"");
		shell.append("cat /home/oracle/.bash_profile");
		shell.append("echo \"*******************************************************\"");
		shell.append("cat /etc/sysctl.conf");
		shell.append("echo \"*******************************************************\"");
		shell.append("cat /etc/security/limits.conf");
		shell.append("echo \"*******************************************************\"");
		shell.append("df -h");
		shell.append("echo \"*******************************************************\"");
		shell.append("cat /etc/selinux/config");
		shell.append("echo \"*******************************************************\"");
		if(para.getOsCode() == 6){
			shell.append("/etc/init.d/iptables status");
		}
		else{
			shell.append("systemctl status firewalld.service");
		}
		shell.append("echo \"*******************************************************\"");
		shell.append("sysctl -p");
		//如果不是创建安装环境
		if(!para.getInstallContent().equals("environment")){
			shell.append("rm -rf /home/oracle/database");
			shell.append("rm -f /home/oracle/.db_install.rsp");
			shell.append("rm -f /home/oracle/temp.log");
			for(String item : oraclezip)
				shell.append("rm -f /home/oracle/" + item);
			for(String item : oraclezip)
				shell.append("cp " + item + " /home/oracle/");
			shell.append("cp .db_install.rsp /home/oracle/");
			for(String item : oraclezip)
				shell.append("chown oracle:oinstall /home/oracle/" + item);
			shell.append("chown oracle:oinstall /home/oracle/.db_install.rsp");
			shell.append("su - oracle << EOF");
			if(para.getDbtype().equals("12.2.0.2") || para.getDbtype().equals("12.2.0.3")){
				//oracle 18c,19c
				for(String item : oraclezip)
					shell.append("unzip /home/oracle/" + item + " -d " + para.getOraclePath() + "/app/oracle/product/" + para.getVersion() + "/db_1/");
			}
			else{
				//oracle 11g,12c
				for(String item : oraclezip)
					shell.append("unzip /home/oracle/" + item);
				shell.append("chmod +x /home/oracle/database/runInstaller");
				shell.append("chmod +x /home/oracle/database/install/*.sh");
				shell.append("chmod +x /home/oracle/database/install/lsnodes");
				shell.append("chmod +x /home/oracle/database/install/unzip");
				shell.append("chmod +x /home/oracle/database/install/.oui");
			}
			shell.append("exit;");
			shell.append("EOF");
			shell.append("su - oracle << EOF");
			shell.append("touch /home/oracle/temp.log");
			//oracle 18C,19C
			if(para.getDbtype().equals("12.2.0.2") || para.getDbtype().equals("12.2.0.3")){
				shell.append(para.getOraclePath() + "/app/oracle/product/" + para.getVersion() + "/db_1/runInstaller -silent -ignorePrereq -debug -responseFile /home/oracle/.db_install.rsp");
			}
			//oracle 11g,12c
			else{
				shell.append("/home/oracle/database/runInstaller -silent -ignorePrereq -debug -responseFile /home/oracle/.db_install.rsp");
			}
			if(para.getDbtype().equals("11.2.0.4")){
				shell.append("while [ true ] ");
				shell.append("do");
				shell.append("chmod +x /tmp/CVU_11.2.0.4.0_oracle/*;");
				shell.append("sleep 1;");
				shell.append("done");
			}
			else if(para.getDbtype().equals("12.1.0.2")){
				shell.append("while [ true ] ");
				shell.append("do");
				shell.append("chmod +x /tmp/CVU_12.1.0.2.0_oracle/*;");
				shell.append("sleep 1;");
				shell.append("done");
			}
			else if(para.getDbtype().equals("12.2.0.1")){
				shell.append("while [ true ] ");
				shell.append("do");
				shell.append("chmod +x /tmp/CVU_12.2.0.1.0_oracle/*;");
				shell.append("sleep 1;");
				shell.append("done");
			}
			shell.append("tail -f /home/oracle/temp.log");
			shell.append("EOF");
		}
		shell.close();
		shell = null;
	}	

	@SuppressWarnings("unused")
	private class Decompile{
		
	}
}
